<?php

class ShanshenmiaoController extends Controller
{
	public function actions()
	{

	}

	public function actionIndex()
	{
		$this->renderFile("index.tpl", $arrTplData);
	}

	public function actionCrawler()
	{
		$q = Yii::app()->request->getParam('q');
		$p = intval(Yii::app()->request->getParam('p'));
		if(!$q){
			$q = "美女";
		}
		if(!$p){
			$p = 1;
		}

		$tbkReq = new TaobaokeItemsGetRequest();
		$tbkReq->setFields("num_iid,title,nick,pic_url,price,click_url");
		$tbkReq->setKeyword($q);
		$tbkReq->setNick("rocyform");
		$tbkReq->setPageNo($p);

		$tbkResponse = Yii::app()->top->execute($tbkReq);
		/*var_dump($tbkResponse);*/
		$arrTbkItems = $tbkResponse["taobaoke_items"]["taobaoke_item"];

		$modPicture = new Picture();
		foreach ($arrTbkItems as $key => $item) {
			$picture = array(
				"pic_url" => $item['pic_url'],
				"pic_key" => "taobao_".$item['num_iid']."_0",
				"source" => "taobao",
				"click_url" => $item['click_url'],
				"price" => $item['price'],
				"name" => $item['title'],
				"crawl_time" => strtotime("now"),
			);
			echo($item["title"]);
			echo($item["pic_url"]);
			$modPicture->save($picture); 
		}


		$arrTplData["tbkResponse"] = $tbkResponse["taobaoke_items"]["taobaoke_item"];
		$this->renderFile('crawler.tpl', $arrTplData);
	}

	public function actionRateFront()
	{
		$p = intval( Yii::app()->request->getParam('p', 1) );
		$p = $p < 1 ? 1 : $p;
		$pageSize = 10;

		$modPicture = new PictureFront();
		$modPicture->setParams(array(
			"condition"=>array(
				"is_del"=>0,
				'rate_count' => 0,
			),
			'limit' => $pageSize,
			'offset' => $pageSize * ($p - 1),
		));
		$arrPictureList = $modPicture->getList();
		$arrTplData['count'] = $modPicture->getCount();
		$arrTplData['pageSize'] = $pageSize;
		$arrTplData["arrPictureList"] = $arrPictureList;
		$this->renderFile('rateFront.tpl', $arrTplData);
	}

	public function actionRateScoreFront()
	{
		$level = intval( Yii::app()->request->getParam('level',1));
		$id = intval( Yii::app()->request->getParam('id') );
		if($id){
			$modPicture = new PictureFront();
			$modPicture->setParams(array('id'=>$id));
			$picture = $modPicture->getItem();
			if($picture){
				$picture['rate_count'] = $picture['rate_count'] + 1;
				$picture['score'] = $picture['score'] + ($level * 100) + rand(-10,10);
				$modPicture->save($picture);
			}
		}
	}

	public function actionDeleteFront()
	{
		$id = intval( Yii::app()->request->getParam('id') );
		if($id){
			$modPicture = new PictureFront();
			$modPicture->setParams(array('id'=>$id));
			$picture = $modPicture->getItem();
			if($picture){
				$picture['is_del'] = 1;
				$modPicture->save($picture);
			}
		}
	}

	public function actionList()
	{
		$p = intval( Yii::app()->request->getParam('p', 1) );
		$p = $p < 1 ? 1 : $p;
		$pageSize = 50;

		$modPicture = new Picture();
		$modPicture->setParams(array(
			"condition"=>array(
				"is_fail"=>0,
				"is_pass"=>0,
			),
			'limit' => $pageSize,
			'offset' => $pageSize * ($p - 1),
		));
		$arrPictureList = $modPicture->getList();
		$arrTplData['count'] = $modPicture->getCount();
		$arrTplData['pageSize'] = $pageSize;
		$arrTplData["arrPictureList"] = $arrPictureList;
		$this->renderFile("list.tpl", $arrTplData);
	}

	private function savePictureToFile(&$picture){
		$path = Yii::app()->params['pictureFolder'];
		$fileName = md5($picture['pic_url']);
		$ext = strrchr($picture['pic_url'],".");
		//处理没有图片后缀信息的url,例如花瓣的图片
		if(! in_array(strtolower($ext), array('.jpg','.png', '.gif','.jpeg')) ){
			$ext = '.jpg';
		}
		$localPath = $fileName.$ext;
		$picPath = $path.$localPath;
		$picFile = file_get_contents($picture['pic_url']);
		file_put_contents($picPath, $picFile);
		$picture['uniq_sign'] = md5(substr($picFile, 512)).md5($picFile);
		$picture['local_path'] = $localPath;
		$this->setPicShowUrl($picture);
	}

	private function setPicShowUrl(&$picture){
		if( in_array($picture['source'], Yii::app()->params['useLocalPathOnSrc']) ){
			$picture['pic_show_url'] = Yii::app()->params['picturePath'] . $picture['local_path'];
		} else {
			$picture['pic_show_url'] = $picture['pic_url'];
		}
	}

	public function actionRateScore()
	{
		$level = intval( Yii::app()->request->getParam('level',1));
		$id = intval( Yii::app()->request->getParam('id') );

		$modPicture = new Picture();
		$arrToPost = array();
		$modPicture->setParams(array("id"=>$id));
		$picture = $modPicture->getItem();
		$picture["is_pass"] = 1;
		$picture["verify_time"] = strtotime("now");
		
		//Save picture to local.
		$this->savePictureToFile($picture);

		$modPicture->save($picture);
		$picture['bak_id'] = $picture['id'];
		$picture['rate_count'] = $picture['rate_count'] + 1;
		$picture['score'] = $picture['score'] + ($level * 100) + rand(-10,10);
	
		$arrToPost[] = $picture;		
		$postData = array("pics"=>json_encode($arrToPost));
		echo json_encode($arrToPost);
		Yii::app()->curl->post(Yii::app()->params['remoteUrl'], $postData);
	}

	public function actionPass()
	{
		$ids = Yii::app()->request->getParam("ids");
		$arrId = explode(",", $ids);
		$modPicture = new Picture();
		$arrToPost = array();

		foreach ($arrId as $key => $id) {
			$modPicture->setParams(array("id"=>$id));
			$picture = $modPicture->getItem();
			$picture["is_pass"] = 1;
			$picture["verify_time"] = strtotime("now");
			$picture['bak_id'] = $picture['id'];

			//Save picture to local.
			$this->savePictureToFile($picture);

			$modPicture->save($picture);

			$arrToPost[] = $picture;
		}
		$postData = array("pics"=>json_encode($arrToPost));
		#echo(Yii::app()->curl->get("http://127.0.0.1"));
		#echo(Yii::app()->params['remoteUrl']);
		Yii::app()->curl->post(Yii::app()->params['remoteUrl'], $postData);
	}

	public function actionFail()
	{
		$ids = Yii::app()->request->getParam("ids");
		$arrId = explode(",", $ids);
		$modPicture = new Picture();
		foreach ($arrId as $key => $id) {
			$modPicture->setParams(array("id"=>$id));
			$picture = $modPicture->getItem();
			$picture["is_fail"] = 1;
			$picture["verify_time"] = strtotime("now");
			$modPicture->save($picture);
		}
		echo "OK";
	}


	public function actionInitPicShowUrl(){
		//$modPicture = new Picture();
		$modPictureFront = new PictureFront();
		//$modPictureFront->setParams(array('where'=>' local_path != "" '));
		$picFrontList = $modPictureFront->getList();
		/*
		
		 $picList = $modPicture->getList();
		 foreach ($picList as $key => $item) {
			$this->setPicShowUrl($item);
			$modPicture->save($item);
		}
		*/
		foreach ($picFrontList as $key => $item) {
			$this->setPicShowUrl($item);
			$modPictureFront->save($item);
		}
		echo 'done';
	}
}